#!/bin/bash
#Automated process to import data from a BAM25 into DMS.
use()
{
	echo "Proper use:"
	echo "*	This script should be run from the CARBLog directory" 
	echo "$0 \"/path/to/input/file\""
	exit 1
}
if [ $# == 0 ]; then
{
	use
}
fi

if ! [ -f "$1" ]; then
{
	echo "Sorry, I can't access the file \" $1 \"..."
	use
	
}
fi
#GRAB LOCAL CONFIGURATIONS FOR SITE CODE, ETC
while read line; do
    eval $line
done < Config/config
#INITIALIZE THIS VARIABLE
this_value=''
#CREATE FILE NAME
this_file="${saroad}_backpoll.MIN"
#BLANK THE FILE IN CASE IT ALREADY EXISTS
echo '' >$this_file
#FEEDBACK THE SITE CODE WE ARE PROCESSING
echo "Processing site: ${saroad}"
#DETERMINE ALL POSSIBLE BAMS WE COULD BE USING AT THE MOMENT, THEN ASK THE USER IF THIS IS THE ONE
possible_instruments=`cat Config/config | grep -o [A-Z]*BAM[0-9,A-Z,a-z]*_drv`
for i in ${possible_instruments}
do
	echo "Is $i the instrument that this data is coming from?"
	read response
	if [[ "$response" == [Y,y]* ]]; then
	{
		echo "Processing $i..."
    #VALIDATE THAT THE USER CHOSE THE CORRECT INSTRUMENT BY GRABBING THE HEADERS
		echo "grabbing headers from instrument..."
			inst_port=`cat resources/${i}/.usr1 | grep -oe "-d /dev/tty[A-Z]*[0-9]" | sed 's/-d //g'`
			echo "$i port is $inst_port"
			sleep 5
			inst_baud=`cat resources/${i}/.usr1 | grep -oe "-b [4,9,3,1][0-9]*" | sed 's/-b //g'`
			echo "$i baud is $inst_baud"
			sleep 5
			header=`sjinn -d ${inst_port} -b${inst_baud} -r1000 -s "\r\r\r64" -w4 | grep Time`
			#PREPARE A VARIABLE STRING CONSISTING IF THE INSTRUMENT HEADERS, SO ONE FOR ONE A VALUE IS ASSIGNED
			prepped_headers=`echo "$header" | sed 's/(mg\/m3)//g' | sed 's/,/ /g' | sed 's/(m3)//g' | sed 's/PM2.5/PM25/g' | \
			sed 's/([Kk][Tt][Ss])//g' | sed 's/([Dd][Ee][Gg])//g' | sed 's/(C)//g' | sed 's/(%)//g' | sed 's/Time /DATE TIME /g' | sed 's/Conc(mg)/Conc/g'` 
			
			#echo $prepped_headers
			
			cat $1 | sed 's/,/ /g' | while read `echo $prepped_headers`
			do
				#CALCULATE IF THE ERRORS ARE > 0, IF SO FLAG THAT HOUR
				flag=`echo "${E} + ${U} + ${M} + ${I} + ${L} + ${R} + ${N} + ${F} + ${P} + ${D} + ${C}" | bc`
				if [[ $flag != 0 ]]; then
					{
						op_code="5"
					}
				else
					{
						op_code="0"
					}
				fi	
				#SUBTRACT AN HOUR
        old_date=`date -d "${DATE} ${TIME}" +%s`
				new_date=`echo "$old_date-3600" | bc`
				new_date=`date -d @$new_date "+%m/%d/%Y %H:%M:00"`
				#DETERMINE THE DATA LABEL FOR EACH PARAMETER THIS INSTRUMENT REPORTS
        #, THEN GRAB THE APPROPRIATE DMS LABEL TO REPORT IT BY, FROM
        #THE dms.cfg FILE
				for d in `cat "resources/${i}/.display" | grep -oe 'BAM[0-9a-z=]*' -oe 'QTOT[_a-z0-9=]*'`
				do
					  
						dms_label=`cat sys_resources/dms.cfg | grep -v ^\# | grep "^${d}" | awk '{ print $2 }'`
						case $d in
						BAM[0-9a-z=]*)
							#PUT THE PM25 DATA INTO THE .MIN FILE
							this_value=`echo ${Conc}*1000 | bc`
							echo "${saroad}|${dms_label}|${new_date}|${op_code}|${this_value}|" >> $this_file
						;;
						QTOT[_a-z0-9=]*)
							#PUT THE Q TOTAL DATA INTO THE .MIN FILE
							echo "${saroad}|${dms_label}|${new_date}|${op_code}|$Qtot|" >> $this_file
						;; 
						esac
				done
			done
		echo "converting to windows....."
    #REPLACE CR WITH CR LF
		sed -i 's/$/\r/g' $this_file
    #REMOVE BLANK LINES
		sed -i 's/^$//g' $this_file
		#cat $this_file
		echo "sending to dms...."
		bash sys_resources/carbput.sh "$this_file"
		echo "COMPLETE...check DMS in about 15 minutes"
		exit
		}
		fi
	
done
